package com.five.controller;

import com.five.dto.IdRequest;
import com.five.exception.BusinessException;
import com.five.pojo.Comment;
import com.five.pojo.NovelCategory;
import com.five.pojo.Reader;
import com.five.result.Result;
import com.five.service.ReaderService;
import com.five.vo.NovelRankVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reader")
@Slf4j
public class ReaderController {
    @Autowired
    private ReaderService readerService;


    /**
     * 根据手机号查询读者信息
     * @param phone
     * @return
     */
    @PostMapping("/queryByPhone/{phone}")
    public Result<Reader> queryByPhone(@PathVariable String phone){
        try {
            Reader reader = readerService.queryByPhone(phone);
            return Result.success(reader);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }


    /**
     * 根据邮箱查询读者信息
     * @param email
     * @return
     */
    @PostMapping("/queryByEmail/{email}")
    public Result<Reader> queryByEmail(@PathVariable String email){
        try {
            Reader reader = readerService.queryByEmail(email);
            return Result.success(reader);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     * 根据登陆信息Id查询读者信息
     * @param loginInfoId
     * @return
     */
    @PostMapping("/queryByLoginInfoId/{loginInfoId}")
    public Result<Reader> queryByLoginInfoId(@PathVariable Long loginInfoId){
        try {
            Reader reader = readerService.queryByLoginInfoId(loginInfoId);
            return Result.success(reader);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     * 插入读者信息
     * @param
     * @return
     */
    @PostMapping("/insertReader")
    public Result insertReader(@RequestBody Reader reader){
        try {
           readerService.insertReader(reader);
            return Result.success("新增读者信息成功");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     *更新读者信息
     * @param
     * @return
     */
    @PostMapping("/updateReader")
    public Result updateReader(@RequestBody Reader reader){
        try {
            readerService.updateReader(reader);
            return Result.success("更新读者信息成功");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     *根据账号查询读者信息
     * @param
     * @return
     */
    @PostMapping("/queryByAccount/{account}")
    public Result<Reader> queryByAccount(@PathVariable String account){
        try {
            Reader reader = readerService.queryByAccount(account);
            return Result.success(reader);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     * 根据小说id查询读者对该书的评论及回复
     */
    @PostMapping("/books/comments")
    public Result<List<Comment>> getBooksWithComments(@RequestBody IdRequest request) {
        try {
            List<Comment> data = readerService.getCommentByNovelId(request.getNovelId());
            return Result.success(data);
        } catch (Exception e) {
            log.error("获取书籍评论失败", e);
            return Result.error("获取书籍评论失败: " + e.getMessage());
        }
    }

    /**
     * 根据commentId更新评论的回复数量
     */
    @PostMapping("/update-reply-count")
    public Result<Void> updateReplyCount(@RequestBody IdRequest request) {
        try {
            readerService.updateReplyCount(request.getCommentId());
            return Result.success("更新成功");
        } catch (Exception e) {
            log.error("更新失败", e);
            return Result.error("更新失败: " + e.getMessage());
        }
    }
    /**
     * 根据token查询loginInfo
     * 再通过loginInfoId查询读者性别
     * 根据性别查询分类
     * @param token
     * @return
     */
    @PostMapping("/category")
    public Result<List<NovelCategory>> listCategoryByUserId(@RequestHeader("token") String token) {
        Integer type = readerService.getReaderTypeById(token);
        List<NovelCategory> novelTreeByType = readerService.getNovelTreeByType(Long.valueOf(type));
        return Result.success(novelTreeByType);
    }

    /**
     *查看小说收藏榜前十
     */
    @PostMapping("/rank/collect/top10")
    public Result<List<NovelRankVo>> top10Collect() {
        return Result.success(readerService.rankTop10Collect());
    }

    /*@PostMapping("/listByCategory")
    public Result<List<NovelRankVo>> listByCategory(@RequestBody Map<String, Long> params) {
        Long categoryId = params.get("categoryId");
        return Result.success(readerService.listNovelByCategory(categoryId));
    }*/

    @PostMapping("/listByCategory")
    public Result<List<NovelRankVo>> listByCategory(@RequestBody Map<String, Long> params) {
        Long categoryId = params.get("categoryId");
        return Result.success(readerService.listNovelByCategory(categoryId));
    }
}
